#include <QtGui>

#include "mainwindow.h"


MainWindow::MainWindow() : QWidget(0, Qt::FramelessWindowHint)
{
  quitting = FALSE;
  inButton = FALSE;
  /* TODO: skin!!! */
  setFixedSize(235, 283);
  int radius = 20; //radius of exit button
  exitButton = new QRegion(117 - radius/2, 251 - radius/2,
			   radius, radius, QRegion::Ellipse);
  exitLabel = new CloseButton(this, 112, 241);
  dropArea = new DropArea(this);
  setWindowTitle(tr("Pant"));
  createLayout();
  
  move(100, 100);

  graphwin = new GraphWin(this, Qt::Window);
}
 
void MainWindow::createLayout()
{
 
  /* display cycling-computer image */

  setBackgroundRole(QPalette::Window);	
  setAutoFillBackground(false);
  
  pixmap = new QPixmap(":/images/front2.png");
  //QPalette palette;
  //palette.setBrush(backgroundRole(), QBrush(*pixmap));
  //setPalette(palette);
	
}

void MainWindow::paintEvent(QPaintEvent *)
{
	QPainter painter(this);
	painter.drawPixmap(0, 0, *pixmap);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
  if (event->key() == Qt::Key_W) {
	QString winTitle = "Empty";
	graphwin->setWindowTitle(winTitle);
	graphwin->show();	
  	event->accept();
  }
}
 
void MainWindow::mousePressEvent(QMouseEvent *event)
{
  if (event->button() == Qt::LeftButton) {
    if(exitButton->contains(event->pos())) {
      quitting = TRUE;
      inButton = TRUE;
      dropArea->quitting();
			
    }
    else {
      dragPosition = 
	event->globalPos() - frameGeometry().topLeft();
    }
    event->accept();
  }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
  if (event->buttons() == Qt::LeftButton) {
    if(!quitting) {
      move(event->globalPos() - dragPosition);
    }
    else {
      if (!exitButton->contains(event->globalPos())) {
	inButton = FALSE;
	dropArea->notQuitting();
      }
    }
    event->accept();
  }
}
 
void MainWindow::mouseReleaseEvent(QMouseEvent *)
{
  if(quitting && inButton) {
    QApplication::closeAllWindows(); 
  }
  else {
    quitting = FALSE;
  }
}
 
bool MainWindow::loadFile(const QFileInfo &fi)
{
  QFile file(fi.absoluteFilePath());
  QString fileName = fi.baseName();
  if (!file.open(QFile::ReadOnly | QFile::Text)) {
    QMessageBox::warning(this, tr("Application"),
			 tr("Cannot read file %1:\n%2.")
			 .arg(fileName)
			 .arg(file.errorString()));
    return FALSE;
  }
	 
  openData = new DataHRM(file);

  graphwin->setData(openData);
  graphwin->setWindowTitle(fileName);
  graphwin->move(x() + width() + 20, y());
  graphwin->show();
  graphwin->setGraphWid();

  return TRUE;
}

void MainWindow::unload()
{
  dropArea->unload();
}


void MainWindow::resizeEvent(QResizeEvent * /* event */)
{
 setMask(pixmap->createMaskFromColor(QColor(90,60,90)));
}

CloseButton::CloseButton(QWidget* parent, int x, int y) : QLabel(parent) 
{
  move(x, y);
  resize(12, 20);
  QFont font;
  font.setBold(true);
  setFont(font);
}
 
void CloseButton::enterEvent(QEvent * /* event */)
{
  setText(tr("x"));
}
 
void CloseButton::leaveEvent(QEvent * /* event */)
{
  setText(tr(""));
}
